package common.dao.Impl;

import cn.hutool.core.util.StrUtil;
import common.dao.RoleDao;
import common.domain.Role;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl implements RoleDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(Role role) {
        String sql = "insert into role\n" +
                "        (role_name) \n" +
                "values(?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setString(1,role.getRoleName());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(Role role) {
        String sql = "update role set";
        List<Object> param = new ArrayList<>();
        if (role == null){
            return;
        }else{
            if(role.getRoleId() != null){
                sql += " role_id = ?,";
                param.add(role.getRoleId());
            }
            if(role.getRoleName() != null){
                sql += " role_name = ?,";
                param.add(role.getRoleName());
            }
            if(role.getCreateTime() != null){
                sql += " create_time = ?,";
                param.add(role.getCreateTime());
            }
            if(role.getUpdateTime() != null){
                sql += " update_time = ?,";
                param.add(role.getUpdateTime());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where role_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,role.getRoleId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from role where role_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public Role selectById(Long id) {
        String sql = "select * from role where role_id = ?";
        Role role = new Role();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                role.setRoleId(rs.getLong(1));
                role.setRoleName(rs.getString(2));
                role.setCreateTime(rs.getDate(3));
                role.setUpdateTime(rs.getDate(4));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return role;
    }

    @Override
    public List<Role> selectList(Role role) {
        String sql = "select * from role where 1=1";
        List<Role> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (role == null){
            sql = "select * from role";
        }else{
            if(role.getRoleId() != null){
                sql += " and role_id = ?";
                param.add(role.getRoleId());
            }
            if(role.getRoleName() != null){
                sql += " and role_name like \"%\"?\"%\"";
                param.add(role.getRoleName());
            }
            if(role.getCreateTime() != null){
                sql += " and create_time = ?";
                param.add(role.getCreateTime());
            }
            if(role.getUpdateTime() != null){
                sql += " and update_time = ?";
                param.add(role.getUpdateTime());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                Role role1 = new Role();
                role1.setRoleId(rs.getLong(1));
                role1.setRoleName(rs.getString(2));
                role1.setCreateTime(rs.getDate(3));
                role1.setUpdateTime(rs.getDate(4));
                list.add(role1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
